package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import model.Follow;
import model.Vote;

import org.apache.log4j.Logger;

public class VoteDao extends RpDao {
	private static Logger log = Logger.getLogger(Follow.class);

	public int insert(Vote vo) {
		int n = 0;
		Connection conn=this.getConn();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			// insertVote =
			// "insert into comment(itemid,uid,type)  values(?,?,?)",
//				selectVote = "select count(*) from comment where uid=? and type=?",
			pstmt =conn.prepareStatement(SqlString.selectVote);
			pstmt.setInt(1, vo.getUid());
			pstmt.setInt(2, vo.getCommentid());
			rs = pstmt.executeQuery();
			if (rs.next()) {
				n=rs.getInt("count");
			} 
			if(n==0)
			{
				pstmt = conn.prepareStatement(SqlString.insertVote,
						Statement.RETURN_GENERATED_KEYS);
				int i = 1;
				pstmt.setInt(i++, vo.getItemid());
				pstmt.setInt(i++, vo.getUid());
				pstmt.setInt(i++, vo.getCommentid());

				n = pstmt.executeUpdate();
			}
			else{
				n=-3;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			log.error(e.getMessage());

		} finally {
			this.closeDao(conn,pstmt, rs);
		}
		return n;
	}
}
